home *** CD-ROM | disk | FTP | other *** search
- Path: druid.borland.com!usenet
- From: pete@borland.com (Pete Becker)
- Newsgroups: comp.lang.c++
- Subject: Re: Help! Macro replacement......
- Date: 13 Apr 1996 01:03:07 GMT
- Organization: Borland International
- Message-ID: <4kmugb$r1a@druid.borland.com>
- References: <4kega2$1tk@hptemp1.cc.umr.edu>
- NNTP-Posting-Host: pbecker.borland.com
- Mime-Version: 1.0
- Content-Type: Text/Plain; charset=ISO-8859-1
- X-Newsreader: WinVN 0.99.5
-
- In article <4kega2$1tk@hptemp1.cc.umr.edu>, jlu@cs.umr.edu says...
- >
- >
- >Hi *,
- >
- >I wrote an ugly macro definition which, when I compiled, surprised
- >me. The macro is defined as
- >
- > #define ABS(X) ((X <= 0) ? -X : X)
- >
- >And I called the macro by using
- >
- > int y = ABS(-6+4);
- >
- >I expected y would be 10. However, Solaris CC and Turbo C++ version 3.0
- >for DOS returned compilation error. And IRIX CC and g++ version 2.6.3
- >and 2.7.0 returned 10.
- >
- >The problem is clear that one replacement gives
- >
- > int y = --6+4; // compilation error
- >
- >And the other gives
- >
- > int y = - -6+4; // y = 10
- >
- >I tried to find out the answer from Chap 16 of ANSI C++ standard
- >draft but cannot figure out which one is correct. Will someone
- >please EMAIL me which one conforms to ANSI C++? If interested, I
- >can post the summary. Thanks!
-
- Better yet, I can tell you how to write that macro so that it works with all
- compilers. Just put parentheses around each X in the macro definition:
-
- #define ABS(X) (((X) <= 0) ? -(X) : (X))
-
-